[Framework] MVC架构

Posted by Aerber Zhou on 2018-03-18

MVC架构指的是module view control 模型 视图 控制三层结构,相当于把以前耦合性很高的代码分层三个模块。

UI界面的代码放入view模块中,专门负责和用户连接的界面。

数据处理层的代码放入module模块中来处理和数据增删改的操作,例如mysql数据库的操作。

连接view模块和module模块的控制,相当于把view中用户事件产生的数据通过control模块来传递给module模块中进行数据的操作。


Module模块的以service后缀

View模块的以UI后缀

Control模块的以manager后缀

举个栗子

在一个C#的名叫test的项目集合中包含了三个项目(目前来说)

我们这么命名文件:

LoginFrm —— view模块中的一个类 —— test.USL(User Show Layer)项目中的一个文件

LoginService —— module模块中的一个类 —— test.DAL(Data Access Layer)项目中的一个文件

LoginManager —— control模块中的一个类 —— test.BLL(Business Logic Layer)项目中的一个文件

然后三个项目之间,view和control之间相互添加引用,control和module之间相互添加引用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//LoginFrm.cs
Using Systetm;
Using test.BLL;

Namespace test.USL{
public class LoginFrm : From{

Public LoginFrm(){
//此处初始化操作在C#中程序会自动生成一个initiator函数,内容包括当前窗口上所有控件的实例化和初始化。
Initiator();
}

Public bottom_click(){
LoginManager loginManager = null;
If(loginManager.Login(textbox_user.tostring(),textbox_password.tostring())){
Message.show(“Login success !”);
}
else Message.show(“Login failure!”);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//LoginControl.cs
Using System;
Using test.DAL;
Using test.USL;

Namespace test.BLL{
Public class LoginManager(){
Public bool Login(string usrname, string password){
LoginService loginservce = null;
If(loginservice.login(username, password))
Return true;
Else retrun false;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//LoginService.cs
Using system;
Using test.BLL;

Namespace test.DAl{
Public class LoginManager(){
Public bool login(string usrname, string password){
//进行sql语句查询之类的
//.........

If(password == sql.password)
Return true;
Else return false;
}
}
}

看起来好像是做了两遍一样的功能,但是这样有效的分开了代码,把作用相同的代码放在了一起,便于管理。

然后,后期我们可以通过委托或者static方法来进行优化。